package com.example.myapplication.ui.fragment

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.android.arouter.utils.TextUtils
import com.example.myapplication.R
import com.example.myapplication.arouter.ARouterPath
import com.example.myapplication.ui.vm.SharedViewModel
import com.kun.base.env.Constants
import com.kun.base.ui.AbsBaseFragment
import com.kun.base.util.MMKVUtils
import kotlinx.android.synthetic.main.fragment_mine.*

/**
 * 我的
 *  @author : MyPC
 *  @date : 2021/4/14
 *  @time : 10:38
 */
class MineFragment : AbsBaseFragment() {

    private lateinit var mineViewModel: MineViewModel
    private lateinit var sharedViewModel: SharedViewModel

    override fun getLayoutId(): Int {
        return R.layout.fragment_mine
    }

    @SuppressLint("SetTextI18n")
    override fun initViewModel() {
        sharedViewModel = getApplicationScopeViewModel(SharedViewModel::class.java)
        sharedViewModel.userdata.observe(this, Observer { t ->
            setNickName()
        })
        mineViewModel = getFragmentScopeViewModel(MineViewModel::class.java)
        mineViewModel.coinsRequest.userCoinsResult.observe(this, Observer { t ->
            if (t.errorCode == 0) {
                tv_user_coins!!.text = "积分：" + t.data!!.coinCount
                tv_rank!!.text = "排名：" + t.data!!.rank
                tv_userName!!.text =
                    if (!TextUtils.isEmpty(t.data!!.nickname)) t.data!!.nickname else t.data!!.username
            }
        })
    }

    override fun initView() {
        tv_userName!!.setOnClickListener {
            val userName = MMKVUtils.getString(Constants.USERNAME_CACHE_KEY)
            if (!TextUtils.isEmpty(userName))
                ARouter.getInstance().build(ARouterPath.SETTING).navigation()
            else
                ARouter.getInstance().build(ARouterPath.LOGIN).navigation()
        }

        tv_collection!!.setOnClickListener {
            //我的收藏
            ARouter.getInstance().build(ARouterPath.MY_COLLECTION).navigation()
        }
        tv_question!!.setOnClickListener {
            //问答
            ARouter.getInstance().build(ARouterPath.QUESTION).navigation()
        }
        tv_common_website!!.setOnClickListener {
            //常用网站
            ARouter.getInstance().build(ARouterPath.COMMON_WEBSITE).navigation()
        }
        tv_coins!!.setOnClickListener {
            //积分
            ARouter.getInstance().build(ARouterPath.RANK).navigation()
        }
        tv_user_coins!!.setOnClickListener {
            //积分流水
            ARouter.getInstance().build(ARouterPath.USER_COINS).navigation()
        }

        tv_other_entrance!!.setOnClickListener {
            //其他练习入口
            ARouter.getInstance().build(ARouterPath.OTHER_ENTRANCE).navigation()
        }
        setNickName()
    }


    private fun setNickName() {
        val userName = MMKVUtils.getString(Constants.USERNAME_CACHE_KEY)
        if (!TextUtils.isEmpty(userName)) {
            tv_userName!!.text = userName
            group_info!!.visibility = View.VISIBLE
            mineViewModel.requestUserCoinsResult()
        } else {
            tv_userName!!.text = "请登录！"
            group_info!!.visibility = View.GONE
        }

    }
}